package com.brkj.communityStudy;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;
import com.brkj.communityStudy.model.DS_SystemStruct;
import com.brkj.httpInterface.BaseAjaxParams;
import com.brkj.httpInterface.HttpInterface;
import com.brkj.main.R;
import com.brkj.util.MyApplication;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by colin.ding on 2014/5/20.
 */
public class SystemStruct {

    private Context context;
    private SystemStructItemClick itemClick;
    private View view;
    private ListView sysStructListView;
    private List<DS_SystemStruct> systemStructList;


    public interface SystemStructItemClick {

        void onClick(int topicID, String systemName);
    }

    public SystemStruct(Context context, SystemStructItemClick itemClick) {
        this.context = context;
        this.itemClick = itemClick;

        getSystemStruct();
    }

    public View getView() {

        view = LayoutInflater.from(context).inflate(R.layout.system_struct, null);
        sysStructListView = (ListView) view.findViewById(R.id.sysStructListView);

        return view;
    }


    private void getSystemStruct() {
        BaseAjaxParams params = new BaseAjaxParams();
        params.put(HttpInterface.HIF_GetSystemStruct.params.UserID, MyApplication.myUserID);

        FinalHttp fh = new FinalHttp();
        fh.post(HttpInterface.HIF_GetSystemStruct.address, params, new AjaxCallBack<Object>() {

            @Override
            public void onSuccess(Object t) {
                super.onSuccess(t);

                Gson gson = new Gson();
                Type listType = new TypeToken<List<DS_SystemStruct>>() {
                }.getType();
                systemStructList = gson.fromJson((String) t, listType);

                if ((systemStructList != null) && (systemStructList.size() > 0)) {
                    SystemStructAdapter adapter = new SystemStructAdapter(context, systemStructList, itemClick);
                    sysStructListView.setAdapter(adapter);
                }
            }
        });
    }


    public class SystemStructAdapter extends BaseAdapter {
        private ListItemView listItemView;
        protected LayoutInflater listContainer;
        private Context context;
        private List<DS_SystemStruct> systemStructList;
        private List<DS_SystemStruct> systemStructListOne = new ArrayList<DS_SystemStruct>();
        private SystemStructItemClick itemClick;

        public SystemStructAdapter(Context context, List<DS_SystemStruct> systemStructList, SystemStructItemClick itemClick) {
            this.context = context;
            this.systemStructList = systemStructList;
            this.itemClick = itemClick;

            for (DS_SystemStruct info : systemStructList) {
                if (info.getParentID() == 0) {
                    systemStructListOne.add(info);
                }
            }

            listContainer = LayoutInflater.from(context);
        }

        private class ListItemView {
            public TextView courseClassName;
            public TextView courseNum;
            public GridView gridView;
        }

        ;

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return systemStructListOne.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // 自定义视图
            listItemView = new ListItemView();
            // 获取list_item布局文件的视图
            if (convertView == null) {
                convertView = listContainer.inflate(R.layout.course_class_info_item, null);
                // 获取控件对象
                listItemView.courseClassName = (TextView) convertView.findViewById(R.id.name);
                listItemView.courseNum = (TextView) convertView.findViewById(R.id.courseNum);
                listItemView.gridView = (GridView) convertView.findViewById(R.id.gridview);

                // 设置控件集到convertView
                convertView.setTag(listItemView);
            } else {
                listItemView = (ListItemView) convertView.getTag();
            }

            listItemView.courseClassName.setText(systemStructListOne.get(position).getName());
            listItemView.courseNum.setText("共" + systemStructListOne.get(position).getTopicNum() + "个专题");

            List<DS_SystemStruct> systemStructListSecond = new ArrayList<DS_SystemStruct>();
            for (DS_SystemStruct info : systemStructList) {
                if (info.getParentID() == systemStructListOne.get(position).getClassId()) {
                    systemStructListSecond.add(info);
                }

            }

            SystemStructSecondAdapter adapter = new SystemStructSecondAdapter(context,
                    systemStructListSecond, itemClick);
            listItemView.gridView.setAdapter(adapter);

            return convertView;
        }


        public class SystemStructSecondAdapter extends BaseAdapter {
            private ListItemView listItemView;
            protected LayoutInflater listContainer;
            private Context context;
            private List<DS_SystemStruct> courseClassList;
            private SystemStructItemClick itemClick;

            public SystemStructSecondAdapter(Context context, List<DS_SystemStruct> courseClassList,
                                             SystemStructItemClick itemClick) {
                this.context = context;
                this.courseClassList = courseClassList;
                this.itemClick = itemClick;

                listContainer = LayoutInflater.from(context);
            }

            private class ListItemView {
                public TextView courseClassName;
                public TextView courseNum;
            }

            ;

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return courseClassList.size();
            }

            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // 自定义视图
                listItemView = new ListItemView();
                // 获取list_item布局文件的视图
                if (convertView == null) {
                    convertView = listContainer.inflate(R.layout.course_class_second_item, null);
                    // 获取控件对象
                    listItemView.courseClassName = (TextView) convertView.findViewById(R.id.courseClassName);
                    listItemView.courseNum = (TextView) convertView.findViewById(R.id.courseNum);

                    // 设置控件集到convertView
                    convertView.setTag(listItemView);
                } else {
                    listItemView = (ListItemView) convertView.getTag();
                }

                listItemView.courseClassName.setText(courseClassList.get(position).getName());

                listItemView.courseNum.setText(Integer.toString(courseClassList.get(position).getTopicNum()));


                convertView.setOnClickListener(new myOnclickListener(position));


                return convertView;
            }

            class myOnclickListener implements View.OnClickListener {
                private int position;

                public myOnclickListener(int position) {
                    this.position = position;
                }


                @Override
                public void onClick(View v) {
                    itemClick.onClick(courseClassList.get(position).getClassId(), courseClassList.get(position).getName());
                }

            }
        }

    }
}
